﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports Mysoft.Map.Data
Imports System.Web.UI.Design
Imports System.Text




<ToolboxData("<{0}:BUProjTree runat=server></{0}:BUProjTree>")> Public Class BUProjTree
    Inherits Mysoft.Map.Application.Controls.AppDropDownTree

    Private _datatable As DataTable

    Private _textControl As System.Web.UI.WebControls.TextBox
    Private _valueControl As System.Web.UI.WebControls.TextBox
    Private _levelControl As System.Web.UI.WebControls.TextBox
    Private _levelTypeControl As System.Web.UI.WebControls.TextBox

    Private _ProjectString As String        '项目过滤字符串

    Public Event ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Public Enum DisplayHiberarchys
        GROUP = 0
        COMPANY = 1
        PROJECT = 2
    End Enum

#Region "定义属性"

    '控件数据源
    <Browsable(False), Category("Data"), Description("控件数据源。")> Overridable Property [DataTable]() As DataTable
        Get
            Return _datatable
        End Get
        Set(ByVal value As DataTable)
            _datatable = value
        End Set
    End Property

    '样式
    <Browsable(False), Category("Data")> Overrides Property CssClass() As String
        Get
            If MyBase.CssClass Is String.Empty Then
                Return "BUProjTree"
            End If
            Return MyBase.CssClass
        End Get
        Set(ByVal value As String)
            MyBase.CssClass = value
        End Set
    End Property

    '控件的值
    <Browsable(False)> Overridable Property [Value]() As String
        Get
            If Not FindControl(Me.ID & "_txtValue") Is Nothing AndAlso TypeOf FindControl(Me.ID & "_txtValue") Is TextBox Then
                Return CType(FindControl(Me.ID & "_txtValue"), TextBox).Text.Trim
            End If
            Return String.Empty
        End Get

        Set(ByVal Value As String)
            If Not FindControl(Me.ID & "_txtValue") Is Nothing AndAlso TypeOf FindControl(Me.ID & "_txtValue") Is TextBox Then
                CType(FindControl(Me.ID & "_txtValue"), TextBox).Text = Value.ToLower
            End If
        End Set
    End Property


    '控件图标的 Url
    <Browsable(False), Category("Appearance"), Description("控件图标的 Url。"), DefaultValue(""), Editor("System.Web.UI.Design.ImageUrlEditor", GetType(System.Drawing.Design.UITypeEditor))> Overridable Property Icon() As String
        Get
            If Not ViewState("Icon") Is Nothing Then
                Return CStr(ViewState("Icon"))
            End If
            Return ""
        End Get
        Set(ByVal Value As String)
            ViewState("Icon") = Value
        End Set
    End Property

    '是否显示图标（默认：True）
    <Category("Data"), Description("是否显示图标（默认：True）。"), DefaultValue(True)> Overridable Property EnableIcon() As Boolean
        Get
            If Not ViewState("EnableIcon") Is Nothing Then
                Return CBool(ViewState("EnableIcon"))
            End If
            Return True
        End Get
        Set(ByVal Value As Boolean)
            ViewState("EnableIcon") = Value
        End Set
    End Property

    '单击列表项，是否展开下级（默认：True）
    <Category("Data"), Description("单击列表项，是否展开下级（默认：True）。"), DefaultValue(True)> Overridable Property ClickExpand() As Boolean
        Get
            If Not ViewState("ClickExpand") Is Nothing Then
                Return CBool(ViewState("ClickExpand"))
            End If
            Return True
        End Get
        Set(ByVal Value As Boolean)
            ViewState("ClickExpand") = Value
        End Set
    End Property


    <Category("Data"), Description("设置显示层次：Project 显示到项目（默认）；Building 显示到楼栋；Company 显示到公司")> Overridable Property DisplayHiberarchy() As DisplayHiberarchys
        Get
            If Not ViewState("DisplayHiberarchy") Is Nothing Then
                Return CStr(ViewState("DisplayHiberarchy"))
            End If
            Return DisplayHiberarchys.GROUP
        End Get
        Set(ByVal Value As DisplayHiberarchys)
            ViewState("DisplayHiberarchy") = Value
        End Set
    End Property

    '设置是否显示项目
    <Category("Data"), Description("设置是否显示项目"), DefaultValue(False)> Overridable Property IsShowProject() As Boolean
        Get
            If Not ViewState("IsShowProject") Is Nothing Then
                Return CStr(ViewState("IsShowProject"))
            End If
            Return False
        End Get
        Set(ByVal Value As Boolean)
            ViewState("IsShowProject") = Value
        End Set
    End Property

    '设置是否显示产品
    <Category("Data"), Description("设置是否显示产品"), DefaultValue(False)> Overridable Property IsShowProduct() As Boolean
        Get
            If Not ViewState("IsShowProduct") Is Nothing Then
                Return CStr(ViewState("IsShowProduct"))
            End If
            Return False
        End Get
        Set(ByVal Value As Boolean)
            ViewState("IsShowProduct") = Value
        End Set
    End Property


    '设置是否只能选择最末级（默认：False）
    <Category("Data"), Description("设置是否只能选择最末级（默认：False）")> Overridable Property IsSelectLastLevelOnly() As Boolean
        Get
            If Not ViewState("IsSelectLastLevelOnly") Is Nothing Then
                Return CBool(ViewState("IsSelectLastLevelOnly"))
            End If
            Return False
        End Get
        Set(ByVal Value As Boolean)
            ViewState("IsSelectLastLevelOnly") = Value
        End Set
    End Property


    '设置层级类型（默认：true）
    <Category("Data"), Description("设置层级类型")> Public Property DefaultType() As String
        Get
            If Not ViewState("DefaultType") Is Nothing Then
                Return ViewState("DefaultType")
            End If
            Return ""
        End Get
        Set(ByVal Value As String)
            ViewState("DefaultType") = Value
        End Set
    End Property

    <Category("Data"), Description("初始展开级别（默认：2级）。"), DefaultValue(True)> Overridable Property DefaultExpandLevel() As Integer
        Get
            If Not ViewState("DefaultExpandLevel") Is Nothing Then
                Return CInt(ViewState("DefaultExpandLevel"))
            End If
            Return 2
        End Get
        Set(ByVal Value As Integer)
            ViewState("DefaultExpandLevel") = Value
        End Set
    End Property

    <Browsable(False), Category("Data"), Description("为列表项提供级别的数据源字段。")> Overridable ReadOnly Property DataLevelField() As String
        Get
            If Not ViewState("DataLevelField") Is Nothing Then
                Return CStr(ViewState("DataLevelField"))
            End If
            Return "LevelCode"
        End Get
        'Set(ByVal Value As String)
        '    ViewState("DataLevelField") = Value
        'End Set
    End Property

    <Browsable(False), Category("Data"), Description("为列表项提供文本内容的数据源字段。")> Overridable ReadOnly Property DataTextField() As String
        Get
            If Not ViewState("DataTextField") Is Nothing Then
                Return CStr(ViewState("DataTextField"))
            End If
            Return "LevelName"
        End Get
        'Set(ByVal Value As String)
        '    ViewState("DataTextField") = Value
        'End Set
    End Property

    <Browsable(False), Category("Data"), Description("为各列表项提供值的数据源字段。")> Overridable ReadOnly Property DataValueField() As String
        Get
            If Not ViewState("DataValueField") Is Nothing Then
                Return CStr(ViewState("DataValueField"))
            End If
            Return "LevelGUID"
        End Get
        'Set(ByVal Value As String)
        '    ViewState("DataValueField") = Value
        'End Set
    End Property
#End Region

#Region "定义属性（前端事件）"

    <Category("Behavior"), Description("控件填充显示内容时触发。"), DefaultValue("")> Overridable Property OnFillContent() As String
        Get
            If Not ViewState("OnFillContent") Is Nothing Then
                Return CStr(ViewState("OnFillContent"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("OnFillContent") = Value
        End Set
    End Property

    <Category("Behavior"), Description("前端事件 ondropdown 触发时调用的前端函数名。"), DefaultValue("")> Overridable Property OnDropDown() As String
        Get
            If Not ViewState("OnDropDown") Is Nothing Then
                Return CStr(ViewState("OnDropDown"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("OnDropDown") = Value
        End Set
    End Property

    <Category("Behavior"), Description("前端事件 onbeforechange 触发时调用的前端函数名。"), DefaultValue("")> Overridable Property OnBeforeChange() As String
        Get
            If Not ViewState("OnBeforeChange") Is Nothing Then
                Return CStr(ViewState("OnBeforeChange"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("OnBeforeChange") = Value
        End Set
    End Property

    <Category("Behavior"), Description("前端事件 onchange 触发时调用的前端函数名。"), DefaultValue("")> Overridable Property OnChange() As String
        Get
            If Not ViewState("OnChange") Is Nothing Then
                Return CStr(ViewState("OnChange"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("OnChange") = Value
        End Set
    End Property

    <Category("Behavior"), Description("前端事件 onquery 触发时调用的前端函数名。"), DefaultValue("")> Overridable Property OnQuery() As String
        Get
            If Not ViewState("OnQuery") Is Nothing Then
                Return CStr(ViewState("OnQuery"))
            End If
            Return String.Empty
        End Get
        Set(ByVal Value As String)
            ViewState("OnQuery") = Value
        End Set
    End Property
#End Region

#Region "配置数据"

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    End Sub

    '设置默认值
    Public Sub SetDefaultValue(Optional ByVal strDefGUID As String = "")
        '显示模式：到集团(Group)、到公司(Company)、到项目(Project)
        Dim strDisplayHiberarchy As String
        Dim dtDefault As DataTable

        strDisplayHiberarchy = Me.DisplayHiberarchy.ToString("G").ToUpper

        Dim strDefGUIDHistory As String
        If Me.Value = "" Or Me.Value Is Nothing Then
            Dim MyGeneral As New Mysoft.Map.Utility.General

            strDefGUIDHistory = MyGeneral.GetCurrArg("GlJsc" & strDisplayHiberarchy).ToUpper
        Else
            strDefGUIDHistory = Me.Value.ToUpper
        End If
        strDefGUID = strDefGUID.ToUpper

        '先清除ProjDDTree2的默认属性，后续再根据strDefGUID赋值
        Me.DefaultLevel = ""
        Me.DefaultValue = ""
        Me.DefaultText = ""
        Me.DefaultType = ""
        Me.Value = ""

        If strDefGUID = "" AndAlso strDefGUIDHistory = "" Then
            _textControl.Text = Me.DefaultText
            _valueControl.Text = Me.DefaultValue
            _levelControl.Text = Me.DefaultLevel
            _levelTypeControl.Text = Me.DefaultType
            Exit Sub
        End If
        'System.Diagnostics.Debugger.Launch()

        Dim strProjGuidlst As String = Mysoft.PubProject.Business.PublicMNG.GetProjectString(Context.Current.Session("UserGUID"), Nothing, "0302", False)
        Dim iDefLevel As Integer = 0
        Dim iDefLevelHistory As Integer = 0
        Dim strDefCode As String
        Dim strDefCodeHistory As String
        Dim strsql As String
        Dim dttemp As DataTable
        Dim blIsDef As Boolean = True

        If strDefGUID <> "" Then
            If strDefGUID.IndexOf(";") = -1 Then
                strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                strsql = strsql + " ELSE '' END) as LevelCode,case when Level = 0 and IsEndCompany =0 then '0' when IsEndCompany=1 then '2' else '1' end as LevelType FROM "
                strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1)) and buguid='" & strDefGUID & "'"
                dttemp = MyDB.GetDataTable(strsql)

                If dttemp.Rows.Count > 0 Then
                    iDefLevel = CInt(dttemp.Rows(0).Item("LevelType"))
                    strDefCode = dttemp.Rows(0).Item("LevelCode").ToString
                Else
                    If ((strDisplayHiberarchy = "GROUP" Or strDisplayHiberarchy = "COMPANY") AndAlso Me.IsShowProject) OrElse strDisplayHiberarchy = "PROJECT" Then
                        strDefCode = MyDB.GetDataItemString("select left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as projcode from p_project where IfEnd=1 and  projguid='" & strDefGUID & "' and projguid in (" & strProjGuidlst & ")")
                        If strDefCode <> "" Then
                            iDefLevel = 3
                        Else
                            If Me.IsShowProduct Then
                                strDefCode = MyDB.GetDataItemString("select convert(varchar(40),projguid)+'.'+productcode as LevelCode from cb_Product where productguid='" & strDefGUID & "'")
                                If strDefCode <> "" Then
                                    iDefLevel = 4
                                End If
                            Else
                                strDefCode = ""
                                iDefLevel = 0
                                strDefGUID = ""
                                strDefGUIDHistory = ""
                            End If
                        End If
                    Else
                        strDefCode = ""
                        iDefLevel = 0
                        strDefGUID = ""
                        strDefGUIDHistory = ""
                    End If
                End If
            Else
                strDefCode = strDefGUID.Split(";")(2)
                iDefLevel = strDefGUID.Split(";")(1)
                strDefGUID = strDefGUID.Split(";")(0)

                If strDefGUID.ToUpper = "11B11DB4-E907-4F1F-8835-B9DAAB6E1F23" Then
                    strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                    strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                    strsql = strsql + " ELSE '' END) as LevelCode,case when Level = 0 and IsEndCompany =0 then '0' when IsEndCompany=1 then '2' else '1' end as LevelType FROM "
                    strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1)) and buguid='" & strDefGUID & "'"
                    dttemp = MyDB.GetDataTable(strsql)

                    If dttemp.Rows.Count > 0 Then
                        iDefLevel = CInt(dttemp.Rows(0).Item("LevelType"))
                        strDefCode = dttemp.Rows(0).Item("LevelCode").ToString
                    Else
                        strDefCode = ""
                        iDefLevel = 0
                        strDefGUID = ""
                        strDefGUIDHistory = ""
                    End If
                End If

                If ((strDisplayHiberarchy = "GROUP" Or strDisplayHiberarchy = "COMPANY") AndAlso Me.IsShowProject) OrElse strDisplayHiberarchy = "PROJECT" Then
                    If Not Me.IsShowProduct AndAlso iDefLevel = 4 Then
                        strDefCode = ""
                        iDefLevel = 0
                        strDefGUID = ""
                        strDefGUIDHistory = ""
                    Else
                        If iDefLevel = 3 AndAlso MyDB.GetDataItemString("select left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as projcode from p_project where IfEnd=1 and  projguid='" & strDefGUID & "' and projguid in (" & strProjGuidlst & ")") = "" Then
                            strDefCode = ""
                            iDefLevel = 0
                            strDefGUID = ""
                            strDefGUIDHistory = ""
                        End If
                    End If
                Else
                    If iDefLevel > 2 Then
                        strDefCode = ""
                        iDefLevel = 0
                        strDefGUID = ""
                        strDefGUIDHistory = ""
                    End If
                End If
            End If
        End If

        If strDefGUIDHistory <> "" Then
            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
            strsql = strsql + " ELSE '' END) as LevelCode,case when Level = 0 and IsEndCompany =0 then '0' when IsEndCompany=1 then '2' else '1' end as LevelType FROM "
            strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1)) and buguid='" & strDefGUIDHistory & "'"
            dttemp = MyDB.GetDataTable(strsql)

            If dttemp.Rows.Count > 0 Then
                iDefLevelHistory = CInt(dttemp.Rows(0).Item("LevelType"))
                strDefCodeHistory = dttemp.Rows(0).Item("LevelCode").ToString
            Else
                If ((strDisplayHiberarchy = "GROUP" Or strDisplayHiberarchy = "COMPANY") AndAlso Me.IsShowProject) OrElse strDisplayHiberarchy = "PROJECT" Then
                    If iDefLevel = 2 Then
                        strDefCodeHistory = MyDB.GetDataItemString("select left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as projcode from p_project where buguid='" & strDefGUID & "' and IfEnd=1 and  projguid='" & strDefGUIDHistory & "' and projguid in (" & strProjGuidlst & ")")
                    Else
                        strDefCodeHistory = MyDB.GetDataItemString("select left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as projcode from p_project where IfEnd=1 and  projguid='" & strDefGUIDHistory & "' and projguid in (" & strProjGuidlst & ")")
                    End If

                    If strDefCodeHistory <> "" Then
                        iDefLevelHistory = 3
                    Else
                        If Me.IsShowProduct Then
                            If iDefLevel = 3 Then
                                strDefCodeHistory = MyDB.GetDataItemString("select convert(varchar(40),projguid)+'.'+productcode as LevelCode from cb_Product where  projguid='" & strDefGUID & "' and  productguid='" & strDefGUIDHistory & "' and projguid in (" & strProjGuidlst & ")")
                            Else
                                strDefCodeHistory = MyDB.GetDataItemString("select convert(varchar(40),projguid)+'.'+productcode as LevelCode from cb_Product where productguid='" & strDefGUIDHistory & "' and projguid in (" & strProjGuidlst & ")")
                            End If

                            If strDefCodeHistory <> "" Then
                                iDefLevelHistory = 4
                            Else
                                strDefGUIDHistory = ""
                            End If
                        Else
                            strDefGUIDHistory = ""
                        End If
                    End If
                Else
                    strDefGUIDHistory = ""
                End If
            End If
        End If

        If strDefGUID = "" Then
            strDefGUID = strDefGUIDHistory
            iDefLevel = iDefLevelHistory
            strDefCode = strDefCodeHistory
        End If

        If strDefGUID = "" Then
            _textControl.Text = Me.DefaultText
            _valueControl.Text = Me.DefaultValue
            _levelControl.Text = Me.DefaultLevel
            _levelTypeControl.Text = Me.DefaultType
            Exit Sub
        End If

        If iDefLevel < iDefLevelHistory Then
            Select Case iDefLevelHistory
                Case 1
                    If iDefLevel = 0 Then
                        blIsDef = False
                    End If
                    Exit Select
                Case 2
                    If iDefLevel = 0 Then
                        blIsDef = False
                    End If
                    If iDefLevel = 1 AndAlso strDefCodeHistory.IndexOf(strDefCode + ".") <> -1 Then
                        blIsDef = False
                    End If
                    Exit Select
                Case 3
                    If iDefLevel = 0 Or iDefLevel = 2 Then
                        blIsDef = False
                    End If

                    If iDefLevel = 1 Then
                        Dim strBuguid As String = MyDB.GetDataItemString("select buguid from p_project where IfEnd=1 and  projguid='" & strDefGUIDHistory & "'")
                        strsql = "select (CASE WHEN level <= 2 THEN HierarchyCode"
                        strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                        strsql = strsql + " ELSE '' END) as LevelCode from MybusinessUnit a where IsEndCompany = 1 and buguid='" & strBuguid & "' and HierarchyCode like '" & strDefCode & ".%'"
                        If MyDB.GetDataItemString(strsql) <> "" Then
                            blIsDef = False
                        End If
                    End If
                    Exit Select
                Case 4
                    If iDefLevel = 0 Or iDefLevel = 3 Then
                        blIsDef = False
                    End If

                    If iDefLevel = 1 Then
                        Dim strBuguid As String = MyDB.GetDataItemString("select buguid from p_project where IfEnd=1 and  projguid='" & Left(strDefCodeHistory, strDefCodeHistory.IndexOf(".")) & "'")
                        strsql = "select (CASE WHEN level <= 2 THEN HierarchyCode"
                        strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                        strsql = strsql + " ELSE '' END) as LevelCode from MybusinessUnit a where IsEndCompany = 1 and buguid='" & strBuguid & "' and HierarchyCode like '" & strDefCode & ".%'"
                        If MyDB.GetDataItemString(strsql) <> "" Then
                            blIsDef = False
                        End If
                    End If

                    If iDefLevel = 2 Then
                        strsql = "select left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as projcode from p_project where buguid='" & strDefGUID & "' and IfEnd=1 and  projguid='" & Left(strDefCodeHistory, strDefCodeHistory.IndexOf(".")) & "' and projguid in (" & strProjGuidlst & ")"
                        If MyDB.GetDataItemString(strsql) <> "" Then
                            blIsDef = False
                        End If
                    End If
                    Exit Select
            End Select
        End If

        If blIsDef = False Then
            strDefGUID = strDefGUIDHistory
            iDefLevel = iDefLevelHistory
            strDefCode = strDefCodeHistory
        End If

        Select Case strDisplayHiberarchy
            Case "GROUP"
                getDafaultTableForGroup(strDefGUID, iDefLevel, strDefCode, dtDefault)
            Case "COMPANY"
                getDafaultTableForCompany(strDefGUID, iDefLevel, strDefCode, dtDefault)
            Case "PROJECT"
                getDafaultTableForProject(strDefGUID, iDefLevel, strDefCode, dtDefault)
        End Select

        If Not dtDefault Is Nothing AndAlso dtDefault.Rows.Count > 0 Then
            If dtDefault.Rows.Count = 1 Then
                Me.DefaultLevel = dtDefault.Rows(0).Item("LevelCode").ToString
                Me.DefaultValue = dtDefault.Rows(0).Item("LevelGUID").ToString
                Me.DefaultText = dtDefault.Rows(0).Item("LevelName").ToString
                Me.DefaultType = dtDefault.Rows(0).Item("LevelType").ToString
                Me.Value = dtDefault.Rows(0).Item("LevelGUID").ToString
            Else
                Dim dv As DataView
                dv = dtDefault.DefaultView
                dv.Sort = "LevelCode asc"

                Me.DefaultLevel = dv(dv.Count - 1).Item("LevelCode").ToString
                Me.DefaultValue = dv(dv.Count - 1).Item("LevelGUID").ToString
                Me.DefaultText = dv(dv.Count - 1).Item("LevelName").ToString
                Me.DefaultType = dv(dv.Count - 1).Item("LevelType").ToString
                Me.Value = dv(dv.Count - 1).Item("LevelGUID").ToString
            End If

            Me.DataTable = dtDefault
        End If

        _textControl.Text = Me.DefaultText
        _valueControl.Text = Me.DefaultValue
        _levelControl.Text = Me.DefaultLevel
        _levelTypeControl.Text = Me.DefaultType
    End Sub


    Sub getDafaultTableForGroup(ByVal strDefGUID As String, ByVal iDefLevel As Integer, ByVal strDefCode As String, ByRef dtdefault As DataTable)
        Dim strsql As String
        Dim dtTemp As DataTable

        If Me.IsShowProject Then
            If Me.IsShowProduct Then '显示产品
                If iDefLevel = 4 Then
                    strsql = "SELECT  buguid,left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefCode.Split(".")(0) & "'"
                    strsql = strsql + " union all "
                    strsql = strsql + "SELECT projguid as buguid,convert(varchar(40),projguid)+'.'+productcode as LevelCode,productguid as LevelGUID,productname as  LevelName,'产品' as LevelType,1 as IfEnd FROM "
                    strsql = strsql + " cb_Product WHERE productguid ='" & strDefGUID & "'"
                    dtdefault = MyDB.GetDataTable(strsql)

                    strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                    strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                    strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " MybusinessUnit where isendcompany=1 and buguid ='" & dtdefault.Select("LevelType='项目'")(0)("buguid").ToString & "'"
                    dtTemp = MyDB.GetDataTable(strsql)
                    If dtdefault IsNot Nothing AndAlso dtdefault.Rows.Count = 2 AndAlso dtTemp IsNot Nothing AndAlso dtTemp.Rows.Count = 1 Then
                        dtdefault.Select("LevelType='项目'")(0)("LevelCode") = dtTemp.Rows(0).Item("LevelCode").ToString & "." & dtdefault.Select("LevelType='项目'")(0)("LevelGUID").ToString
                        dtdefault.Select("LevelType='产品'")(0)("LevelCode") = dtTemp.Rows(0).Item("LevelCode").ToString & "." & dtdefault.Select("LevelType='产品'")(0)("LevelCode")
                    Else
                        dtdefault = Nothing
                        Exit Sub
                    End If

                    If dtTemp.Rows(0)("buguid").ToString.ToUpper = "11B11DB4-E907-4F1F-8835-B9DAAB6E1F23" Then
                        dtdefault.ImportRow(dtTemp.Rows(0))
                        Exit Sub
                    End If

                    Dim strCodeTmp As String
                    Dim strCodelst As String = ""
                    Dim i As Integer
                    Dim iCnt As Integer
                    strCodeTmp = dtTemp.Rows(0)("LevelCode").ToString
                    strCodelst = Left(strCodeTmp, strCodeTmp.IndexOf("."))
                    strCodeTmp = Mid(strCodeTmp, strCodelst.Length + 2)
                    If strCodeTmp.IndexOf(".") <> -1 Then
                        strCodelst = "'" & strCodelst & "','" & strCodelst & "." & Left(strCodeTmp, strCodeTmp.IndexOf(".")) & "'"
                    Else
                        strCodelst = "'" & strCodelst & "'"
                    End If

                    strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                    strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                    strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                    strsql = strsql + " and (buguid ='" & dtTemp.Rows(0)("buguid").ToString & "' or HierarchyCode in (" & strCodelst & "))"
                    dtTemp = MyDB.GetDataTable(strsql)
                    If dtTemp IsNot Nothing Then
                        iCnt = dtTemp.Rows.Count - 1
                        For i = 0 To iCnt
                            dtdefault.ImportRow(dtTemp.Rows(i))
                        Next
                    Else
                        dtdefault = Nothing
                        Exit Sub
                    End If
                End If

                If Not Me.IsSelectLastLevelOnly Then '任意级公司、项目、产品
                    Select Case iDefLevel
                        Case 0
                            strsql = " SELECT buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM mybusinessunit  WHERE buguid='11B11DB4-E907-4F1F-8835-B9DAAB6E1F23'"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                        Case 1
                            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                            strsql = strsql + " and (HierarchyCode in ('" & Left(strDefCode, strDefCode.IndexOf(".")) & "','" & strDefCode & "'))"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                        Case 2
                            Dim strCodeTmp As String
                            Dim strCodelst As String = ""
                            Dim i As Integer
                            Dim iCnt As Integer
                            strCodeTmp = strDefCode
                            If strCodeTmp.IndexOf(".") <> -1 Then
                                strCodelst = Left(strCodeTmp, strCodeTmp.IndexOf("."))
                                strCodeTmp = Mid(strCodeTmp, strCodelst.Length + 2)
                                If strCodeTmp.IndexOf(".") <> -1 Then
                                    strCodelst = "'" & strCodelst & "','" & strCodelst & "." & Left(strCodeTmp, strCodeTmp.IndexOf(".")) & "'"
                                Else
                                    strCodelst = "'" & strCodelst & "'"
                                End If
                            Else
                                strCodelst = "'" & strCodeTmp & "'"
                            End If

                            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                            strsql = strsql + " and (buguid ='" & strDefGUID & "' or HierarchyCode in (" & strCodelst & "))"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                        Case 3
                            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit where isendcompany=1 and bucode ='" & strDefCode.Split(".")(0) & "'"
                            dtTemp = MyDB.GetDataTable(strsql)

                            If Not (dtTemp IsNot Nothing AndAlso dtTemp.Rows.Count = 1) Then
                                dtdefault = Nothing
                                Exit Sub
                            End If

                            Dim strCodeTmp As String
                            Dim strCodelst As String = ""
                            Dim i As Integer
                            Dim iCnt As Integer
                            strCodeTmp = dtTemp.Rows(0)("LevelCode").ToString
                            If strCodeTmp.IndexOf(".") <> -1 Then
                                strCodelst = Left(strCodeTmp, strCodeTmp.IndexOf("."))
                                strCodeTmp = Mid(strCodeTmp, strCodelst.Length + 2)
                                If strCodeTmp.IndexOf(".") <> -1 Then
                                    strCodelst = "'" & strCodelst & "','" & strCodelst & "." & Left(strCodeTmp, strCodeTmp.IndexOf(".")) & "'"
                                Else
                                    strCodelst = "'" & strCodelst & "'"
                                End If
                            Else
                                strCodelst = "'" & strCodeTmp & "'"
                            End If

                            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                            strsql = strsql + " and (buguid ='" & dtTemp.Rows(0)("buguid").ToString & "' or HierarchyCode in (" & strCodelst & "))"
                            strsql = strsql + " union all "
                            strsql = strsql + "SELECT  buguid,left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefGUID & "'"

                            dtdefault = MyDB.GetDataTable(strsql)
                            If Not dtdefault Is Nothing AndAlso dtdefault.Select("LevelType='项目'").Length = 1 AndAlso dtdefault.Select("LevelType='公司'").Length = 1 Then
                                dtdefault.Select("LevelType='项目'")(0)("LevelCode") = dtdefault.Select("LevelType='公司'")(0)("LevelCode") & "." & strDefGUID
                            Else
                                dtdefault = Nothing
                                Exit Sub
                            End If
                            Exit Select
                    End Select
                End If
            Else '显示项目
                If iDefLevel = 3 Then
                    strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                    strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                    strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " MybusinessUnit where isendcompany=1 and bucode ='" & strDefCode.Split(".")(0) & "'"
                    dtTemp = MyDB.GetDataTable(strsql)

                    If Not (dtTemp IsNot Nothing AndAlso dtTemp.Rows.Count = 1) Then
                        dtdefault = Nothing
                        Exit Sub
                    End If

                    Dim strCodeTmp As String
                    Dim strCodelst As String = ""
                    Dim i As Integer
                    Dim iCnt As Integer
                    strCodeTmp = dtTemp.Rows(0)("LevelCode").ToString
                    If strCodeTmp.IndexOf(".") <> -1 Then
                        strCodelst = Left(strCodeTmp, strCodeTmp.IndexOf("."))
                        strCodeTmp = Mid(strCodeTmp, strCodelst.Length + 2)
                        If strCodeTmp.IndexOf(".") <> -1 Then
                            strCodelst = "'" & strCodelst & "','" & strCodelst & "." & Left(strCodeTmp, strCodeTmp.IndexOf(".")) & "'"
                        Else
                            strCodelst = "'" & strCodelst & "'"
                        End If
                    Else
                        strCodelst = "'" & strCodeTmp & "'"
                    End If

                    strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                    strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                    strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                    strsql = strsql + " and (buguid ='" & dtTemp.Rows(0)("buguid").ToString & "' or HierarchyCode in (" & strCodelst & "))"
                    strsql = strsql + " union all "
                    strsql = strsql + "SELECT  buguid,left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,1 as IfEnd FROM "
                    strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefGUID & "'"

                    dtdefault = MyDB.GetDataTable(strsql)
                    If Not dtdefault Is Nothing AndAlso dtdefault.Select("LevelType='项目'").Length = 1 AndAlso dtdefault.Select("LevelType='公司'").Length = 1 Then
                        dtdefault.Select("LevelType='项目'")(0)("LevelCode") = dtdefault.Select("LevelType='公司'")(0)("LevelCode") & "." & strDefGUID
                    Else
                        dtdefault = Nothing
                        Exit Sub
                    End If
                End If

                If Not Me.IsSelectLastLevelOnly Then '任意级项目及公司
                    Select Case iDefLevel
                        Case 0
                            strsql = " SELECT buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM mybusinessunit  WHERE buguid='11B11DB4-E907-4F1F-8835-B9DAAB6E1F23'"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                        Case 1
                            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                            strsql = strsql + " and (HierarchyCode in ('" & Left(strDefCode, strDefCode.IndexOf(".")) & "','" & strDefCode & "'))"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                        Case 2
                            Dim strCodeTmp As String
                            Dim strCodelst As String = ""
                            Dim i As Integer
                            Dim iCnt As Integer
                            strCodeTmp = strDefCode
                            If strCodeTmp.IndexOf(".") <> -1 Then
                                strCodelst = Left(strCodeTmp, strCodeTmp.IndexOf("."))
                                strCodeTmp = Mid(strCodeTmp, strCodelst.Length + 2)
                                If strCodeTmp.IndexOf(".") <> -1 Then
                                    strCodelst = "'" & strCodelst & "','" & strCodelst & "." & Left(strCodeTmp, strCodeTmp.IndexOf(".")) & "'"
                                Else
                                    strCodelst = "'" & strCodelst & "'"
                                End If
                            Else
                                strCodelst = "'" & strCodeTmp & "'"
                            End If

                            strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                            strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                            strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                            strsql = strsql + " and (buguid ='" & strDefGUID & "' or HierarchyCode in (" & strCodelst & "))"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                    End Select

                End If
            End If
        Else '显示公司
            If iDefLevel = 2 Then
                Dim strCodeTmp As String
                Dim strCodelst As String = ""
                Dim i As Integer
                Dim iCnt As Integer
                strCodeTmp = strDefCode
                If strCodeTmp.IndexOf(".") <> -1 Then
                    strCodelst = Left(strCodeTmp, strCodeTmp.IndexOf("."))
                    strCodeTmp = Mid(strCodeTmp, strCodelst.Length + 2)
                    If strCodeTmp.IndexOf(".") <> -1 Then
                        strCodelst = "'" & strCodelst & "','" & strCodelst & "." & Left(strCodeTmp, strCodeTmp.IndexOf(".")) & "'"
                    Else
                        strCodelst = "'" & strCodelst & "'"
                    End If
                Else
                    strCodelst = "'" & strCodeTmp & "'"
                End If

                strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                strsql = strsql + " and (buguid ='" & strDefGUID & "' or HierarchyCode in (" & strCodelst & "))"
                dtdefault = MyDB.GetDataTable(strsql)
            End If

            If Not Me.IsSelectLastLevelOnly Then '末级公司
                Select Case iDefLevel
                    Case 0
                        strsql = " SELECT buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                        strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                        strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM mybusinessunit  WHERE buguid='11B11DB4-E907-4F1F-8835-B9DAAB6E1F23'"
                        dtdefault = MyDB.GetDataTable(strsql)
                        Exit Select
                    Case 1
                        strsql = "SELECT  buguid,(CASE WHEN level <= 2 THEN HierarchyCode"
                        strsql = strsql + " WHEN level > 2 THEN left(HierarchyCode,charindex('.',HierarchyCode)+charindex('.',substring(HierarchyCode,charindex('.',HierarchyCode)+1,500)))+BUCode "
                        strsql = strsql + " ELSE '' END) as LevelCode,buguid as LevelGUID,buname as LevelName,case when Level = 0 and IsEndCompany =0 then '集团' when IsEndCompany=1 then '公司' else '区域' end as LevelType,0 as IfEnd FROM "
                        strsql = strsql + " MybusinessUnit  where ((Level = 0) OR (Level = 1 AND IsCompany = 1) OR (IsEndCompany = 1))"
                        strsql = strsql + " and (HierarchyCode in ('" & Left(strDefCode, strDefCode.IndexOf(".")) & "','" & strDefCode & "'))"
                        dtdefault = MyDB.GetDataTable(strsql)
                        Exit Select
                End Select

            End If
        End If
    End Sub

    Sub getDafaultTableForCompany(ByVal strDefGUID As String, ByVal iDefLevel As Integer, ByVal strDefCode As String, ByRef dtdefault As DataTable)
        Dim strsql As String
        Dim dtTemp As DataTable

        If Me.IsShowProject Then
            If Me.IsShowProduct Then '显示产品 
                If iDefLevel = 4 Then
                    strsql = "SELECT  buguid,left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefCode.Split(".")(0) & "'"
                    strsql = strsql + " union all "
                    strsql = strsql + "SELECT projguid as buguid,convert(varchar(40),projguid)+'.'+productcode as LevelCode,productguid as LevelGUID,productname as  LevelName,'产品' as LevelType,1 as IfEnd FROM "
                    strsql = strsql + " cb_Product WHERE productguid ='" & strDefGUID & "'"
                    dtdefault = MyDB.GetDataTable(strsql)

                    If Not dtdefault Is Nothing AndAlso dtdefault.Rows.Count = 2 Then
                        strsql = "SELECT  buguid,bucode as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                        strsql = strsql + " MybusinessUnit where isendcompany=1 and buguid ='" & dtdefault.Select("LevelType='项目'")(0)("buguid").ToString & "'"
                        dtTemp = MyDB.GetDataTable(strsql)

                        If Not dtTemp Is Nothing AndAlso dtTemp.Rows.Count = 1 Then
                            dtdefault.ImportRow(dtTemp.Rows(0))
                            dtdefault.Select("LevelType='产品'")(0)("LevelCode") = dtTemp.Rows(0).Item("LevelCode").ToString & "." & dtdefault.Select("LevelType='产品'")(0)("LevelCode")
                        Else
                            dtdefault = Nothing
                            Exit Sub
                        End If
                    Else
                        dtdefault = Nothing
                        Exit Sub
                    End If
                End If

                If Not Me.IsSelectLastLevelOnly Then '任意级
                    Select Case iDefLevel
                        Case 2
                            strsql = "SELECT  buguid,bucode as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit where isendcompany=1 and buguid ='" & strDefGUID & "'"
                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                        Case 3
                            strsql = "SELECT  buguid,bucode as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " MybusinessUnit where isendcompany=1 and bucode ='" & strDefCode.Split(".")(0) & "'"
                            strsql = strsql + " union all "
                            strsql = strsql + "SELECT  buguid,left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,0 as IfEnd FROM "
                            strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefGUID & "'"

                            dtdefault = MyDB.GetDataTable(strsql)
                            Exit Select
                    End Select
                End If
            Else '显示项目
                If iDefLevel = 3 Then
                    strsql = "SELECT  buguid,bucode as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " MybusinessUnit where isendcompany=1 and bucode ='" & strDefCode.Split(".")(0) & "'"
                    strsql = strsql + " union all "
                    strsql = strsql + "SELECT  buguid,left(projcode,charindex('.',projcode))+convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,1 as IfEnd FROM "
                    strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefGUID & "'"

                    dtdefault = MyDB.GetDataTable(strsql)
                End If

                If Not Me.IsSelectLastLevelOnly Then  '末级项目
                    If iDefLevel = 2 Then
                        strsql = "SELECT  buguid,bucode as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,0 as IfEnd FROM "
                        strsql = strsql + " MybusinessUnit where isendcompany=1 and buguid ='" & strDefGUID & "'"

                        dtdefault = MyDB.GetDataTable(strsql)
                    End If
                End If
            End If
        Else '显示公司
            If iDefLevel = 2 Then
                strsql = "SELECT  buguid,bucode as LevelCode,buguid as LevelGUID,buname as LevelName,'公司' as LevelType,1 as IfEnd FROM "
                strsql = strsql + " MybusinessUnit where isendcompany=1 and buguid ='" & strDefGUID & "'"
                dtdefault = MyDB.GetDataTable(strsql)
            End If
        End If
    End Sub

    Sub getDafaultTableForProject(ByVal strDefGUID As String, ByVal iDefLevel As Integer, ByVal strDefCode As String, ByRef dtdefault As DataTable)
        Dim strsql As String

        If Me.IsShowProduct Then '显示产品
            If iDefLevel = 4 Then
                strsql = "SELECT  convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,0 as IfEnd FROM "
                strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefCode.Split(".")(0) & "'"
                strsql = strsql + " union all "
                strsql = strsql + "SELECT  convert(varchar(40),projguid)+'.'+productcode as LevelCode,productguid as LevelGUID,productname as  LevelName,'产品' as LevelType,1 as IfEnd FROM "
                strsql = strsql + " cb_Product WHERE productguid ='" & strDefGUID & "'"

                dtdefault = MyDB.GetDataTable(strsql)
            End If

            If Not Me.IsSelectLastLevelOnly Then '末级产品
                If iDefLevel = 3 Then
                    strsql = "SELECT  convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,0 as IfEnd FROM "
                    strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefGUID & "'"

                    dtdefault = MyDB.GetDataTable(strsql)
                End If
            End If
        Else '显示项目
            If iDefLevel = 3 Then
                strsql = "SELECT  convert(varchar(40),projguid) as LevelCode,projguid as LevelGUID,REPLACE(ProjName,'-','') as  LevelName,'项目' as LevelType,1 as IfEnd FROM "
                strsql = strsql + " p_project where IfEnd=1 and projguid ='" & strDefGUID & "'"
                dtdefault = MyDB.GetDataTable(strsql)
            End If
        End If
    End Sub
#End Region

#Region "控件输出"

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        RenderBeginTag(writer)
        RenderContents(writer)
        RenderEndTag(writer)
    End Sub

    '输出控件开始标记
    Public Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
        If Me.DisplayHiberarchy = DisplayHiberarchys.GROUP AndAlso Me.IsShowProject = False Then
            If MyDB.GetDataItemString("select buname from MybusinessUnit where isendcompany=1 and buguid ='11B11DB4-E907-4F1F-8835-B9DAAB6E1F23'") <> "" Then
                Me.Enabled = False
            End If
        End If

        If Me.DisplayHiberarchy = DisplayHiberarchys.COMPANY AndAlso Me.IsShowProject = False Then
            Me.Enabled = False
        End If

        If Me.DisplayHiberarchy = DisplayHiberarchys.PROJECT AndAlso Me.IsShowProduct = False Then
            Me.Enabled = False
        End If

        writer.Write("<table class=""" & Me.CssClass & """ style=""table-layout:fixed;display:inline;background-color:white;")

        '控件宽度
        writer.Write("width:" & IIf(Me.Width.IsEmpty, "100%", Me.Width.ToString) & ";")
        writer.Write(""" cellspacing=""0"" cellpadding=""0"" ID=""" & Me.ID & """")

        '属性
        writer.Write(" datatextfield=""" & System.Web.HttpUtility.HtmlEncode(Me.DataTextField) & """")
        writer.Write(" datavaluefield=""" & System.Web.HttpUtility.HtmlEncode(Me.DataValueField) & """")
        writer.Write(" datalevelfield=""" & System.Web.HttpUtility.HtmlEncode(Me.DataLevelField) & """")

        writer.Write(" clickexpand=""" & IIf(Me.ClickExpand, "true", "false") & """")

        writer.Write(" defaultexpandlevel=""" & Me.DefaultExpandLevel & """")
        writer.Write(" enabled=""" & IIf(Me.Enabled, "true", "false") & """")

        If Me.Icon <> "" Then
            writer.Write(" icon=""" & System.Web.HttpUtility.HtmlEncode(Me.Icon) & """")
        End If

        writer.Write(" enableicon=""" & Me.EnableIcon.ToString.ToLower & """")

        writer.Write(" isselectlastlevelonly=""" & Me.IsSelectLastLevelOnly.ToString.ToLower & """")
        writer.Write(" displayhiberarchy=""" & System.Web.HttpUtility.HtmlEncode(Me.DisplayHiberarchy.ToString.ToLower) & """")
        writer.Write(" isshowproject=""" & Me.IsShowProject.ToString.ToLower & """")
        writer.Write(" isshowproduct=""" & Me.IsShowProduct.ToString.ToLower & """")


        '事件
        If Me.OnFillContent <> "" Then writer.Write(" onfillcontent=""" & System.Web.HttpUtility.HtmlEncode(Me.OnFillContent) & """")
        If Me.OnDropDown <> "" Then writer.Write(" ondropdown=""" & System.Web.HttpUtility.HtmlEncode(Me.OnDropDown) & """")
        If Me.OnBeforeChange <> "" Then writer.Write(" onbeforechange=""" & System.Web.HttpUtility.HtmlEncode(Me.OnBeforeChange) & """")
        If Me.OnChange <> "" Then writer.Write(" onchange=""" & System.Web.HttpUtility.HtmlEncode(Me.OnChange) & """")
        writer.Write(" onquery=""" & System.Web.HttpUtility.HtmlEncode(Me.OnQuery) & """")

        '其它属性
        Dim strAttr As String
        For Each strAttr In Attributes.Keys
            writer.Write(" " & strAttr & "=""" & System.Web.HttpUtility.HtmlEncode(Attributes.Item(strAttr)) & """")
        Next

        '默认项目属性
        If Me.DefaultValue <> "" Then

            Dim dr() As DataRow
            Dim dtTemp As DataTable
            dtTemp = Me.DataTable
            dr = dtTemp.Select("LevelGUID='" & Me.DefaultValue & "'")
            If dr.Length > 0 Then
                writer.Write(" ifend=""" & dr(0)("ifend").ToString & """")
            End If
        End If

        writer.Write(">")
        writer.Write("<col><col width=18>")
        writer.Write("<tr>")
    End Sub

    Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
        '控件内容
        writer.Write("<td onclick=""if (event.srcElement.tagName == 'NOBR' || event.srcElement.name == '_spanOut') " & Me.ID & "_imgSelect.click();""><span name=""_spanOut"" style=""padding-left:5px;BORDER: #7b9ebd 1px solid; FONT-SIZE: 9pt; BORDER-RIGHT: none; WIDTH: 100%; FONT-FAMILY: 宋体, Tahoma, Verdana, Arial; HEIGHT: 19px;cursor:default;"">")
        RenderContent(writer)
        writer.Write("</span></td>")

        '控件下拉按钮
        RenderDropDown(writer)

        '生成子控件
        Me.EnsureChildControls()
        MyBase.RenderContents(writer)
    End Sub

    '输出控件显示内容
    Protected Overridable Sub RenderContent(ByVal writer As System.Web.UI.HtmlTextWriter)
        '默认项目属性
        Dim dtTemp As DataTable
        dtTemp = Me.DataTable


        If Not dtTemp Is Nothing AndAlso dtTemp.Rows.Count > 0 Then
            Dim dv As DataView
            dtTemp = Me.DataTable
            dv = dtTemp.DefaultView
            dv.Sort = "LevelCode asc"
            Dim i As Integer
            Dim iCnt As Integer
            iCnt = dv.Count - 2

            '输出图标
            If Me.EnableIcon AndAlso Me.Icon <> "" Then
                writer.Write("<img src=""" & Me.Icon & """ style=""height:16px;width:16px;vertical-align:middle;margin-left:2px;margin-right:5px;"">")
            End If
            For i = 0 To iCnt
                writer.Write("<span")
                If Not Me.IsSelectLastLevelOnly Then
                    writer.Write(" onclick=""" & Me.ID & ".navigate()"" style=""cursor:hand;color:blue;text-decoration:underline;""")
                    writer.Write(" oid=""" & dv(i).Item("levelguid").ToString.ToLower & """")
                    writer.Write(" levelcode=""" & System.Web.HttpUtility.HtmlEncode(dv(i).Item("levelcode").ToString) & """")
                    writer.Write(" levelguid=""" & dv(i).Item("levelguid").ToString.ToLower & """")
                    writer.Write(" levelname=""" & System.Web.HttpUtility.HtmlEncode(dv(i).Item("levelname").ToString) & """")
                    writer.Write(" leveltype=""" & dv(i).Item("leveltype").ToString & """")
                    writer.Write(" ifend=""" & dv(i).Item("ifend").ToString & """")
                End If
                writer.Write(">" & System.Web.HttpUtility.HtmlEncode(dv(i).Item("LevelName").ToString) & "</span>")

                writer.Write("<span>－</span>")
            Next
            iCnt = dv.Count - 1
            writer.Write("<span onclick=""" & Me.ID & ".navigate()"" style=""cursor:hand;color:blue;text-decoration:underline;""")
            writer.Write(" oid=""" & dv(iCnt).Item("levelguid").ToString.ToLower & """")
            writer.Write(" levelcode=""" & System.Web.HttpUtility.HtmlEncode(dv(iCnt).Item("levelcode").ToString) & """")
            writer.Write(" levelguid=""" & dv(iCnt).Item("levelguid").ToString.ToLower & """")
            writer.Write(" levelname=""" & System.Web.HttpUtility.HtmlEncode(dv(iCnt).Item("levelname").ToString) & """")
            writer.Write(" leveltype=""" & dv(iCnt).Item("leveltype").ToString & """")
            writer.Write(" ifend=""" & dv(iCnt).Item("ifend").ToString & """")
            writer.Write(">" & System.Web.HttpUtility.HtmlEncode(dv(iCnt).Item("LevelName").ToString) & "</span>")
        End If

        ''下拉框选中项
        'If _textControl.Text <> "" Then
        '    If Me.Icon <> "" Then
        '        writer.Write("<img src=""" & Me.Icon & """ style=""height:16px;width:16px;vertical-align:middle;margin-left:2px;margin-right:5px;"">")
        '    End If

        '    writer.Write("<span style=""cursor:hand;color:blue;text-decoration:underline;"">" & System.Web.HttpUtility.HtmlEncode(_textControl.Text) & "</span>")
        'End If
    End Sub

    '输出控件下拉图标
    Protected Overridable Sub RenderDropDown(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.Write("<td><img name=""" & Me.ID & "_imgSelect"" onmouseover=""if (!disabled) src='/_imgs/selectOn.gif';"" onmouseout=""if (!disabled) src='/_imgs/selectOff.gif';"" style=""cursor:hand;"" align=""absmiddle"" src=""/_imgs/selectOff.gif""")
        '下拉事件
        writer.Write(" onclick=""" & Me.ID & ".dropDown()""")
        writer.Write("></td></tr>")

        writer.Write("<tr style=""display:none""><td>")
        writer.Write("<xml id=""_" & Me.ID & "_Xml"">")

        writer.Write("</xml>")
        writer.Write("</td><td>")
    End Sub

    '输出控件结束标记
    Public Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.Write("</td></tr></table>")
    End Sub

    '创建子控件，用于保存选中的值和文本
    Protected Overrides Sub CreateChildControls()
        Controls.Clear()
        ClearChildViewState()

        _textControl = New System.Web.UI.WebControls.TextBox
        _textControl.ID = Me.ID & "_txtText"
        _textControl.Text = DefaultText
        _textControl.Style.Add("display", "none")
        Controls.Add(_textControl)

        _valueControl = New System.Web.UI.WebControls.TextBox
        _valueControl.ID = Me.ID & "_txtValue"
        _valueControl.Text = DefaultValue
        _valueControl.Style.Add("display", "none")
        AddHandler _valueControl.TextChanged, AddressOf _ValueChanged
        Controls.Add(_valueControl)


        _levelControl = New System.Web.UI.WebControls.TextBox
        _levelControl.ID = "txtDefLevel"
        _levelControl.Text = DefaultLevel
        _levelControl.Style.Add("display", "none")
        Controls.Add(_levelControl)

        _levelTypeControl = New System.Web.UI.WebControls.TextBox
        _levelTypeControl.ID = "txtDefLevelType"
        _levelTypeControl.Text = DefaultType
        _levelTypeControl.Style.Add("display", "none")
        Controls.Add(_levelTypeControl)

        TrackViewState()
        ChildControlsCreated = True
    End Sub

#End Region

#Region "其它"

    Private Sub _ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim txtValue As System.Web.UI.WebControls.TextBox

        txtValue = CType(sender, System.Web.UI.WebControls.TextBox)

        Me.Value = txtValue.Text

        RaiseEvent ValueChanged(sender, e)
    End Sub

#End Region

End Class
